package com.yndsoft.jc.supplier.order;

import android.content.Context;
import android.content.Intent;
import android.graphics.PointF;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yndsoft.jc.R;
import com.yndsoft.jc.netutils.DensityUtil;
import com.yndsoft.jc.supplier.adapter.PreviewPicturePagerAdapter;
import com.yndsoft.jc.supplier.base.SupplierBaseActivity;
import com.yndsoft.jc.supplier.model.OrderDetailModel;
import com.yndsoft.jc.supplier.utils.Constants;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class PreviewPictureActivity extends SupplierBaseActivity implements View.OnClickListener {

    private ViewPager previewPictureVp;
    private TextView previewPictureIndicator;
    private int size;
    private boolean isClose;
    private PointF startPoint = new PointF();

    @Override
    public int getContentResId() {
        return R.layout.activity_preview_picture;
    }

    public static void start(Context context, int position, ArrayList<String> data) {
        Intent intent = new Intent(context, PreviewPictureActivity.class);
        intent.putExtra(Constants.PREVIEW_PICTURE_POSITION, position);
        intent.putStringArrayListExtra(Constants.PREVIEW_PICTURE_LIST,  data);
        context.startActivity(intent);
    }

    @Override
    public void initView() {
        previewPictureVp = findViewById(R.id.preview_picture_vp);
        previewPictureIndicator = findViewById(R.id.preview_picture_indicator);
        RelativeLayout previewPictureBack = findViewById(R.id.preview_picture_back);
        previewPictureBack.setOnClickListener(this);
        setListener();
    }

    @Override
    public void initData() {
        int index = getIntent().getIntExtra(Constants.PREVIEW_PICTURE_POSITION, 0);
        previewPictureVp.setCurrentItem(index);
        ArrayList<String> imgList = getIntent().getStringArrayListExtra(Constants.PREVIEW_PICTURE_LIST);
        if (imgList != null) {
            size = imgList.size();
            if (size > 0) {
                imgList.remove(size - 1); // 去掉"+"
                ImageView[] imageViews = new ImageView[size];
                previewPictureVp.setAdapter(new PreviewPicturePagerAdapter(mContext, imgList, imageViews));
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.preview_picture_back:
                finish();
                break;
        }
    }

    public void setListener() {

        previewPictureVp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                previewPictureIndicator.setText((position + 1) + " / " + (size - 1));
            }

            @Override
            public void onPageSelected(int position) {
                previewPictureIndicator.setText((position + 1) + " / " + (size - 1));
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        previewPictureVp.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        isClose = true;
                        startPoint.set(motionEvent.getX(), motionEvent.getY());
                        break;
                    case MotionEvent.ACTION_MOVE:
                        if (Math.abs((motionEvent.getX() - startPoint.x)) > DensityUtil.dip2px(PreviewPictureActivity.this, 5)
                                || Math.abs((motionEvent.getY() - startPoint.y)) > DensityUtil.dip2px(PreviewPictureActivity.this, 5)) {
                            isClose = false;
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        if (isClose) {
                            finish();
                        }
                        break;

                    default:
                        break;
                }
                return false;
            }
        });
    }
}
